/***************************************************************************
 *   Copyright (C) 2007 by Ziman & Xloverin                                *
 *   ziman@centrum.sk, xloverin@gmail.com                                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <QMessageBox>
#include <QStringList>
#include <QThread>

#include "editor.h"
#include "terminal.h"
#include "core.h"
#include "preferences.h"
#include "nicklist.h"

class SleeperThread : public QThread
{
public:
static void msleep(unsigned long msecs)
{
QThread::msleep(msecs);
}
};

Editor::Editor(QWidget *parent, Core *core, Terminal *mainTerminal)
		: QWidget(parent), Ui::Editor()
{
	setupUi(this);
	this->core = core;
	this->mainTerminal = mainTerminal;
	
	// set font
	const Preferences::Prefs &prefs = core->preferences();
	textEdit->setHtml(
		"<html><head><title>term</title><style type=\"text/css\">"
		"body {"
		"font-size: " + QString::number(prefs.appearance.termFontSize) + "px; " + 
		"font-family: '" + prefs.appearance.termFontFace + "'; " +
		// "color: white; background-color: black; "
		"}"
		"</style></head><body></body></html>"
	);
	setAttribute(Qt::WA_DeleteOnClose, true);
	
	// fill in the Addressee list
	QStringList nicks = core->nickList().list();
	cbAddressee->insertItems(0, nicks);
}

#define SEND(x) core->getConnection()->write(x)
#define SLEEP(msec) SleeperThread::msleep(msec)
void Editor::send()
{
	QString addressee = cbAddressee->lineEdit()->text().trimmed();
	QStringList lines = textEdit->toPlainText().split('\n');
	
	QString cmd;
	switch (cbAction->currentIndex()) {
		case 0: cmd = ".smail " + addressee; break;
		case 1: cmd = ".write"; break;
		case 2: cmd = ".wjoke"; break;
		case 3: cmd = ".suggest"; break;
		case 4: cmd = ".complain"; break;
		default: return;
	}
	
	SEND(cmd + "\r\n");
	qApp->processEvents();
	SLEEP(100);
	for (int i = 0; i < lines.size(); ++i) {
		SEND(lines[i] + "\r\n");
		qApp->processEvents();
		SLEEP(100);
	}
	SEND(".\r\n");
	qApp->processEvents();
	SLEEP(100);
	SEND("u\r\n");
}

void Editor::on_cbAction_currentIndexChanged(int index)
{
	cbAddressee->setEnabled(index == 0);
}

void Editor::clear()
{
	textEdit->setText("");
}

QString Editor::text()
{
	return textEdit->toPlainText();
}

void Editor::on_buttonBox_accepted()
{
	QString text = textEdit->toPlainText();
	QString stripped = text; stripDiacritics(stripped);
	
	if (stripped != text) {
		QMessageBox::StandardButton button = QMessageBox::warning(
			this, tr("Diacritics"), tr("This text contains diacritics, which cannot be sent to the talker. "
			"Do you wish to strip it?"), QMessageBox::Yes | QMessageBox::No
		);
		
		if (button == QMessageBox::Yes) {
			textEdit->setText(stripped);
			QMessageBox::information(this, tr("Diacritics"), tr("Diacritics have been stripped. "
				"Please check the result and then click OK again.")
			);
		}
		return;
	}
	
	if (cbAddressee->lineEdit()->text().trimmed() == "") {
		QMessageBox::critical(this, tr("Addressee"), tr("No or invalid addressee given."));
		return;
	}
	
	send();
	close();
}

Editor::~Editor()
{
}
